<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>电影管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/js/lay-module/dtree/dtree.css}">
    <link rel="stylesheet" th:href="@{/layuimini/js/lay-module/dtree/font/dtreefont.css}">

</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"><i class="layui-icon layui-icon-addition"></i>上传电影</button>
            </div>
        </script>

        <table class="layui-table" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="del">下架</a>
        </script>

    </div>
</div>

<div id="dataFormModal" style="display: none;padding: 10px">
    <form class="layui-form layuimini-form" id="dataForm" lay-filter="dataFormFilter">

        <input type="hidden" name="id">

        <div class="layui-form-item">
            <label class="layui-form-label required">电影名称</label>
            <div class="layui-input-block">
                <input type="text" name="movieName" lay-verify="required" placeholder="请输入电影名称" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">上映年份</label>
            <div class="layui-input-block">
                <input type="text" name="movieYear" lay-verify="required" placeholder="请输入上映年份" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电影评分</label>
            <div class="layui-input-block">
                <input type="text" name="movieScore" lay-verify="required" placeholder="请输入电影评分" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">总时长</label>
            <div class="layui-input-block">
                <input type="text" name="movieTime" lay-verify="required" placeholder="请输入电影总时长" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电影类型</label>
            <div class="layui-input-block">
                <input type="radio" name="movieType" value="1" title="爱情" checked="">
                <input type="radio" name="movieType" value="2" title="战争">
                <input type="radio" name="movieType" value="3" title="喜剧">
                <input type="radio" name="movieType" value="4" title="科幻">
                <input type="radio" name="movieType" value="5" title="恐怖">
                <input type="radio" name="movieType" value="6" title="动作">
                <input type="radio" name="movieType" value="7" title="动画">
                <input type="radio" name="movieType" value="8" title="惊悚">
                <input type="radio" name="movieType" value="9" title="魔幻">
                <input type="radio" name="movieType" value="10" title="青春">
                <input type="radio" name="movieType" value="11" title="犯罪">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">主演</label>
            <div class="layui-input-block">
                <input type="text" name="movieAuthor" lay-verify="required" placeholder="请输入主演" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">地区</label>
            <div class="layui-input-block">
                <input type="radio" name="movieRegion" value="华语" title="华语" checked="">
                <input type="radio" name="movieRegion" value="美国" title="美国">
                <input type="radio" name="movieRegion" value="欧洲" title="欧洲">
                <input type="radio" name="movieRegion" value="韩国" title="韩国">
                <input type="radio" name="movieRegion" value="日本" title="日本">
                <input type="radio" name="movieRegion" value="泰国" title="泰国">
                <input type="radio" name="movieRegion" value="香港地区" title="香港地区">
                <input type="radio" name="movieRegion" value="印度" title="印度">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">宣传海报链接</label>
            <div class="layui-input-block">
                <input type="text" name="movieImg" lay-verify="required" placeholder="请输入海报链接" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">电影简介</label>
            <div class="layui-input-block">
                <input placeholder="请输入内容" name="movieDescription" class="layui-input"></input>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="dataFormSubmit">保存</button>
                <button type="button" class="layui-btn layui-btn-primary" id="dataFormReset">取消</button>
            </div>
        </div>
    </form>
</div>

<div id="detailFormModal" style="display: none;padding: 10px">
    <form class="layui-form" id="detailForm" lay-filter="detailFormFilter">
        <div class="layui-form-item">
            <label class="layui-form-label">电影名称</label>
            <div class="layui-input-block">
                <input type="text" name="movieName" readonly class="layui-input layui-input-view" style="border: none">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">上映年份</label>
            <div class="layui-input-block">
                <input type="text" name="movieYear" readonly class="layui-input layui-input-view" style="border: none">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电影评分</label>
            <div class="layui-input-block">
                <input type="text" name="movieScore" readonly class="layui-input layui-input-view" style="border: none">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">总时长</label>
            <div class="layui-input-block">
                <input type="text" name="movieTime" readonly class="layui-input layui-input-view" style="border: none">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">主演</label>
            <div class="layui-input-block">
                <input name="movieAuthor" readonly class="layui-input layui-input-view" style="border: none"></input>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地区</label>
            <div class="layui-input-block">
                <input name="movieRegion" readonly class="layui-input layui-input-view" style="border: none"></input>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">宣传海报链接</label>
            <div class="layui-input-block">
                <input name="movieImg" readonly class="layui-input layui-input-view" style="border: none"></input>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">电影简介</label>
            <div class="layui-input-block">
                <input name="movieDescription" readonly class="layui-input layui-input-view" style="border: none"></input>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-primary" id="detailFormReset">关闭</button>
            </div>
        </div>
    </form>
</div>

<!--xml-select 解决layui多选的解决方案(从后端拿数据)-->
<script th:src="@{/layuimini/js/lay-module/xm-select/xm-select.js}"></script>
<script th:src="@{/layuimini/lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=2.0.0}" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','dtree'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        let active = {
            dataModelType: 1,
            openDataFormModal() {
                let title = active.dataModelType == 1 ? '上传电影' : '编辑电影资料';
                layer.open({
                    title: title
                    , type: 1
                    , content: $('#dataFormModal')
                    , area: ['1150px', '650px']
                    , maxmin: true
                    , cancel: function () {
                        active.closeDataFormModal();
                    }
                });
            },
            closeDataFormModal() {
                $('#dataForm')[0].reset();
                layer.closeAll();
            }
        };

        table.render({
            elem: '#currentTableId',
            method: 'post',
            url: '/system/movieInfo/listByTable',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            height: "full-" + 86,
            page: true,
            limit: 15,
            limits: [15, 20, 25],
            cols: [ [
                {type: "checkbox", width: 50},
                {type: 'numbers',title: '序号'},
                {field: 'movieName',  title: '电影名称'},
                {
                    field: 'movieType', title: '类型',templet: function (d) {
                        let sexArr = ['', '爱情', '战争', '喜剧', '科幻','恐怖','动作','动画','惊悚','魔幻','青春','犯罪'];
                        return sexArr[d.movieType];
                    }
                },
                {field: 'movieRegion', title: '地区'},
                {field: 'movieScore', title: '评分'},
                {field: 'movieAuthor',width:250, title: '主演'},
                {field: 'movieTime',title: '时长(min)'},
                {field: 'movieYear',title: '年份(Year)'},
                {title: '操作', width: 250,align:"center", toolbar: '#currentTableBar', align: "center"}
            ]]
        });

        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                active.dataModelType = 1;
                active.openDataFormModal();
            }
        });

        /**
         * 编辑、删除
         */
        table.on('tool(currentTableFilter)',function (obj) {
            let data = obj.data;
            if(obj.event === 'detail'){
                let sexArr = ['', '爱情', '战争', '喜剧', '科幻','恐怖','动作','动画','惊悚','魔幻','青春','犯罪'];
                data.movieType = sexArr[data.movieType];
                form.val('detailFormFilter',data);
                layer.open({
                    title: '详细信息',
                    type: 1,
                    content: $('#detailFormModal'),
                    area: ['1150px', '650px'],
                    maxmin: true
                });
            }else if(obj.event === 'edit'){
                //显示当前行的数据
                active.dataModelType = 2;
                form.val('dataFormFilter',data);
                active.openDataFormModal();
            }else if(obj.event === 'del'){
                layer.confirm('确定下架嘛?',function (index) {
                    $.ajax({
                        type: 'post',
                        url: '/system/movieInfo/deleteById',
                        data: {id: data.id},
                        dataType: 'json',
                        success: function (res) {
                            if (res.code == 200) {
                                layer.msg(res.msg, {time: 500, icon: 1}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(res.msg, {time: 500, icon: 2});
                            }
                        },
                        error: function (res){
                            layer.msg('请求失败',{time: 500, icon: 2});
                        }
                    });
                });
            }
        });

        // 监听 add表单 关闭
        $('#dataFormReset').on('click', function () {
            active.closeDataFormModal();
        });

        // 监听 detail表单 关闭
        $('#detailFormReset').on('click', function () {
            $('#detailForm')[0].reset();
            layer.closeAll();
        });

        //监听表单提交
        form.on('submit(dataFormSubmit)', function (data) {
            $.ajax({
                type: 'post',
                url: active.dataModelType == 1 ? '/system/movieInfo/save' : '/system/movieInfo/updateById',
                dataType: 'json',
                data: data.field,
                success: function (res) {
                    if (res.code == 200) {
                        layer.msg(res.msg, {time: 500, icon: 1}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {time: 500, icon: 2});
                    }
                },
                error: function (res) {
                    layer.msg("请求失败", {time: 500, icon: 2});
                }
            });
            return false;
        });

    });
</script>

</body>
</html>